import axios from "axios";
import qs from 'qs'
// import { URL_MAP } from "../config/config";
import { URL_MAP, BACKEND_URL } from "@/config/config";
// import { getDevData, getPersonInfoList } from "./dev-data"

// URL_MAP

export const backendUrl = BACKEND_URL
// const geoServerUrl = '/geoserver/'

export const instance = axios.create({
    baseURL: backendUrl
})


export const fileDownload = axios.create({
    baseURL: ''
})

export async function register(username, phone, passwd) {
    let response = await instance.post(URL_MAP.REGISTER, {
        'username': username,
        'phone': phone,
        'password': passwd,
        // 'uuid': uuid,
        // 'captch': capthe
    })

    let data = await response.data
    console.log(data)
    // data.phoneNumber = username
    // data.username = username
    // let token = data.token
    // instance.defaults.headers['Authorization'] = token;

    // response = await instance.get('current/')
    // data = await response.data
    // data.token = token
    return data
}

export async function login(username, passwd, uuid, capthe) {
    let response = await instance.post(URL_MAP.LOG_IN, {
        'phone': username,
        'password': passwd,
        // 'uuid': uuid,
        // 'captch': capthe
    })

    let data = await response.data.data
    console.log(data)
    // data.phoneNumber = username
    // data.username = username
    let token = data.token
    instance.defaults.headers['Authorization'] = token;

    // response = await instance.get('current/')
    // data = await response.data
    // data.token = token
    return data
}

export function setAuth(auth) {
    instance.defaults.headers['Authorization'] = auth;
}

export async function logout() {
    sessionStorage.clear();

    instance.defaults.headers['Authorization'] = ''
}

export async function getData(url, json) {
    /////////////////////// development //////////////////////////////////
    // let ddata= getDevData(url, json)
    // return ddata

    /////////////////////// production ///////////////////////////////////
    let response = await instance.get(url, {
        params: json
        ,
        paramsSerializer: function (params) {
            return qs.stringify(params, { arrayFormat: 'repeat' })
        }
    })

    let data = await response.data
    return data
}

export async function postJSON(url, json) {
    const headerJSON = {
        "Content-Type": "application/json"
    };
    let response = await instance.post(url, JSON.stringify(json), { headers: headerJSON })
    let data = await response.data
    return data
}

export async function putJSON(url, json) {
    const headerJSON = {
        "Content-Type": "application/json"
    };
    let response = await instance.put(url, JSON.stringify(json), { headers: headerJSON })
    let data = await response.data
    return data
}

export async function patchJSON(url, json) {
    const headerJSON = {
        "Content-Type": "application/json"
    };
    let response = await instance.patch(url, JSON.stringify(json), { headers: headerJSON })
    let data = await response.data
    return data
}

export async function deleteJSON(url, json) {
    const headerJSON = {
        "Content-Type": "application/json"
    };
    let response = await instance.delete(url, { data: json })
    let data = await response.data
    return data
}


export async function initConfigData(metedata) {
    // if(window.FORM_CONFIG.isInit) {
    //     return { code: 0 }
    // }
    let data = await getData(URL_MAP.METADATA, { name: metedata })
    if(data.code === 0) {
        data = data.data
    }
    else{
        return { code: 1 }
    }
    window.FORM_CONFIG.form = window.FORM_CONFIG.form || {}
    window.FORM_CONFIG.config = window.FORM_CONFIG.config || {}
    for(let key in data.form) {
        window.FORM_CONFIG.form[key] = data.form[key]
    }
    for(let key in data.config) {
        window.FORM_CONFIG.config[key] = data.config[key]
    }
  
    // window.FORM_CONFIG.isInit = true
    sessionStorage.removeItem('FORM_CONFIG')
    sessionStorage.setItem('FORM_CONFIG', JSON.stringify(FORM_CONFIG))

    return { code: 0 }
}